﻿using log4net;
using Mate.Caching;
using Mate.Core.Base;
using Mate.Framework.BaseController.Api;
using Mate.Other.Api.Controller.Models.AppHomeMenu;
using Mate.Other.Service;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Mate.Core.Configuration;
using Microsoft.AspNetCore.Mvc;

namespace Mate.Other.Api.Controller.Controller
{
  
    public class AppHomeMenuController: AppHomeMenuBaseController
    {
        private readonly ILog log;
        private readonly ICacheManager cache;
        private readonly MateConfig mateConfig;
        private readonly IAppHomeMenuService appHomeMenuService;

        public AppHomeMenuController(ILog log, ICacheManager cache, MateConfig mateConfig, IAppHomeMenuService appHomeMenuService)
        {
            this.log = log;
            this.cache = cache;
            this.mateConfig = mateConfig;
            this.appHomeMenuService = appHomeMenuService;
        }

        #region 获取首页菜单
        /// <summary>
        /// 获取首页菜单
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public BaseResult<List<AppHomeMenuDto>> HomeList(AppHomeMenuRequest req)
        {
            BaseResult<List<AppHomeMenuDto>> result = new BaseResult<List<AppHomeMenuDto>>();

            string key = "mate.app.home.menu.list";
            var list = cache.Get<List<AppHomeMenuDto>>(key);
            if (list != null)
            {
                result.ResultCode = ResultCode.Success;
                result.Message = "OK";
                result.Data = list;
                return result;
            }

            var appHomeMenuList = appHomeMenuService.Queryable
                .Where(x => x.is_delete == 1 && x.is_open == 1)
                .ToList();
            if (appHomeMenuList == null)
            {
                result.ResultCode = ResultCode.Success;
                result.Message = "暂无相关数据";
                return result;
            }

            result.ResultCode = ResultCode.Success;
            result.Message = "OK";
            result.Data = appHomeMenuList.Select(x => new AppHomeMenuDto()
            {
                iframesrc = x.isframe == 1 ? x.link_url : string.Empty,
                imgsrc = x.menu_icon,
                isiframe = x.isframe,
                name = x.menu_name,
                routerlink = x.isframe == 0 ? x.link_url : string.Empty
            }).Take(req.top).ToList();
            cache.Set(key, result.Data, mateConfig.DefaultCacheTime);

            return result;

        }
        #endregion




    }
}
